home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 October / Macformat17.cdr / Shareware City / Developers / strip-fat-c / Source Kit / Source Kit ƒ / Strip68K / main.cpp / main.cpp
Encoding:
C/C++ Source or Header  |  1994-06-20  |  11.8 KB  |  582 lines  |  [TEXT/MMCC]

  1. /*    main.cpp
  2.  *
  3.  *        This is the main entry point for the code to strip the 68K resources
  4.  *    from a fat binary. This code will help reduce the amount of space that
  5.  *    is used inside of an application on disk.
  6.  *
  7.  *        Code created by 
  8.  *                William Edward Woody
  9.  *                In Phase Consulting
  10.  *                337 West California #4
  11.  *                Glendale, CA 91203
  12.  */
  13.  
  14. #include "Strip.h"
  15.  
  16. #include <AppleEvents.h>
  17. #include <GestaltEqu.h>
  18.  
  19. /************************************************************************/
  20. /*                                                                        */
  21. /*    Globals.                                                            */
  22. /*                                                                        */
  23. /************************************************************************/
  24.  
  25. int FAppleEvents;
  26. int FHasFSSpec;
  27. int FColorQuickdraw;
  28. AEEventHandlerUPP PAppleEventProcessProc;
  29. int QuitFlag = 0;
  30. int RunEventLoop = 0;
  31. int SawNewEvent = 0;
  32. int    DoneEvent;
  33.  
  34. /************************************************************************/
  35. /*                                                                        */
  36. /*    Generic application initialization.                                    */
  37. /*                                                                        */
  38. /************************************************************************/
  39.  
  40. /*    InitMacintoshOS
  41.  *
  42.  *        This does the required minimum of initialization for the Macintosh
  43.  *    operating system.
  44.  */
  45.  
  46. static void InitMacintoshOS(void)
  47. {
  48.     InitGraf(&qd.thePort);                    /*    Init quickdraw display    */
  49.     InitFonts();                            /*    Initialize fonts        */
  50.     InitWindows();                            /*    Initialize windows        */
  51.     InitMenus();                            /*    Initialize menu mangler    */
  52.     TEInit();                                /*    Initialize text editor    */
  53.     InitDialogs(NULL);                        /*    Initialize dialog box    */
  54.  
  55.     FlushEvents(everyEvent,0);                /*    Flush all events        */
  56.     InitCursor();                            /*    Put up arrow cursor        */
  57. }
  58.  
  59. /*    InitMenuBar
  60.  *
  61.  *        This initializes the menu bar
  62.  */
  63.  
  64. static void InitMenuBar(void)
  65. {
  66.     MenuHandle mh;
  67.     Handle menuBar;
  68.  
  69.     menuBar = GetNewMBar(128);
  70.     SetMenuBar(menuBar);
  71.     
  72.     mh = GetMenu(MApple);
  73.     AddResMenu(mh,'DRVR');
  74.  
  75.     DrawMenuBar();
  76. }
  77.  
  78. /*    GetMacintoshEnvironment
  79.  *
  80.  *        Get the current operating system environment state
  81.  */
  82.  
  83. static void GetMacintoshEnvironment(void)
  84. {
  85.     long val;
  86.     
  87.     /*
  88.      *    Color Quickdraw?                    -- System 5.0 or greater
  89.      */
  90.      
  91.     if (!Gestalt(gestaltQuickdrawVersion,&val)) {
  92.         FColorQuickdraw = (val >= 0x100) ? 1 : 0;
  93.     } else {
  94.         FColorQuickdraw = 0;
  95.     }
  96.  
  97.     /*
  98.      *    Apple Events?                        -- System 7.0 or greater
  99.      */
  100.      
  101.     if (!Gestalt(gestaltAppleEventsAttr,&val)) {
  102.         FAppleEvents = (val & (1 << gestaltAppleEventsPresent)) ? 1 : 0;
  103.     } else {
  104.         FAppleEvents = 0;
  105.     }
  106.     
  107.     /*
  108.      *    FSSpec files?                        -- System 7.0 or greater
  109.      */
  110.      
  111.     if (!Gestalt(gestaltFSAttr,&val)) {
  112.         FHasFSSpec = (val & (1 << gestaltHasFSSpecCalls)) ? 1 : 0;
  113.     } else {
  114.         FHasFSSpec = 0;
  115.     }
  116. }
  117.  
  118. /************************************************************************/
  119. /*                                                                        */
  120. /*    System 7 (Apple Event) support.                                        */
  121. /*                                                                        */
  122. /************************************************************************/
  123.  
  124. /*    GotRequiredParams
  125.  *
  126.  *        Were all of the required parameters returned?
  127.  */
  128.  
  129. static OSErr GotRequiredParams(AppleEvent *event)
  130. {
  131.     OSErr myErr;
  132.     long size;
  133.     DescType returnType;
  134.     
  135.     myErr = AEGetAttributePtr(event,keyMissedKeywordAttr,typeWildCard,&returnType,NULL,0,&size);
  136.     if (myErr == noErr) return errAEParamMissed;
  137.     else if (myErr == errAEDescNotFound) return noErr;
  138.     else return myErr;
  139. }
  140.  
  141. /*    AppleEventProcessProc
  142.  *
  143.  *        This is the procedure used to process an apple event.
  144.  */
  145.  
  146. static pascal OSErr AppleEventProcessProc(AppleEvent *event, AppleEvent *, long)
  147. {
  148.     OSErr myErr;
  149.     long eClass,eType;
  150.     DescType returnType;
  151.     long size;
  152.     long off,len;
  153.     AEDescList docList;
  154.     AEKeyword keyWord;
  155.     FSSpec file;
  156.     
  157.     /*
  158.      *    Get the Apple Event message type
  159.      */
  160.     
  161.     if (noErr != (myErr = AEGetAttributePtr(event,keyEventClassAttr,typeType,&returnType,(Ptr)&eClass,sizeof(eClass),&size))) return myErr;
  162.     if (noErr != (myErr = AEGetAttributePtr(event,keyEventIDAttr,typeType,&returnType,(Ptr)&eType,sizeof(eType),&size))) return myErr;
  163.     
  164.     /*
  165.      *    We've now got the class and type of the event. Dispatch for the
  166.      *    common four entry points.
  167.      */
  168.     
  169.     if (eClass == kCoreEventClass) {
  170.         switch (eType) {
  171.             case kAEOpenApplication:
  172.                 /*
  173.                  *    New application event
  174.                  */
  175.                 
  176.                 myErr = GotRequiredParams(event);
  177.                 if (myErr == noErr) {
  178.                     SawNewEvent = 1;
  179.                     InitMenuBar();
  180.                 }
  181.                 return myErr;
  182.             case kAEOpenDocuments:
  183.                 /*
  184.                  *    Open new documents
  185.                  */
  186.                 
  187.                 if (noErr != (myErr = AEGetParamDesc(event,keyDirectObject,typeAEList,&docList))) return myErr;
  188.                 if (noErr != (myErr = AECountItems(&docList,&len))) {
  189.                     AEDisposeDesc(&docList);
  190.                     return myErr;
  191.                 }
  192.                 for (off = 1; off <= len; off++) {
  193.                     if (noErr != (myErr = AEGetNthPtr(&docList,off,typeFSS,&keyWord,&returnType,(Ptr)&file,sizeof(file),&size))) {
  194.                         AEDisposeDesc(&docList);
  195.                         return myErr;
  196.                     }
  197.                     
  198.                     StripFile(&file);
  199.                     if (!SawNewEvent) RunEventLoop = 0;    // Stop running on first NULL
  200.                 }
  201.                 AEDisposeDesc(&docList);
  202.                 break;
  203.             case kAEQuitApplication:
  204.                 /*
  205.                  *    Quit this application
  206.                  */
  207.                 
  208.                 myErr = GotRequiredParams(event);
  209.                 if (myErr == noErr) {
  210.                     QuitFlag = 1;
  211.                     return noErr;
  212.                 } else return myErr;
  213.             default:
  214.                 return errAEEventNotHandled;            // Not able to handle event
  215.         }
  216.     
  217.         return GotRequiredParams(event);                // Fall through to handle this        
  218.     }
  219.     
  220.     /*
  221.      *    Process the rest of the possible events
  222.      */
  223.     
  224.     return errAEEventNotHandled;
  225. }
  226.  
  227. /*    InitAppleEvents
  228.  *
  229.  *        This does the required initialization to install the above routine
  230.  *    into the list of apple event procedures
  231.  */
  232.  
  233. static void InitAppleEvents(void)
  234. {
  235.     PAppleEventProcessProc = NewAEEventHandlerProc(AppleEventProcessProc);
  236.     AEInstallEventHandler(typeWildCard,typeWildCard,PAppleEventProcessProc,0,false);
  237. }
  238.  
  239. /*    EndAppleEvents
  240.  *
  241.  *        Dispose of the apple events manager proc
  242.  */
  243.  
  244. static void EndAppleEvents(void)
  245. {
  246.     DisposeRoutineDescriptor(PAppleEventProcessProc);
  247. }
  248.  
  249. /************************************************************************/
  250. /*                                                                        */
  251. /*    About Me Dialog Box Manager                                            */
  252. /*                                                                        */
  253. /************************************************************************/
  254.  
  255. static short GRedraw = 0;
  256.  
  257. /*    DrawVersionString
  258.  *
  259.  *        This function (which can be overridden by the user) draws the
  260.  *  version if the version resource exists
  261.  */
  262.  
  263. static void DrawVersionString(Rect *r)
  264. {
  265.     FontInfo finfo;
  266.     
  267.     struct VersRecord {
  268.         unsigned char    majVersion;
  269.         unsigned char    minVersion;
  270.         unsigned char    devel;
  271.         unsigned char    release;
  272.         short            region;
  273.         unsigned char    shortVers[2];
  274.     } **vers;
  275.     short x,y;
  276.     
  277.     if (NULL == (vers = (VersRecord **)GetResource('vers',1))) return;
  278.     HLock((Handle)vers);
  279.     
  280.     /*
  281.      *  Print the version string (the pascal string contained in this thing)
  282.      */
  283.     
  284.     TextFont(4);
  285.     TextSize(9);
  286.     TextMode(srcOr);
  287.     GetFontInfo(&finfo);
  288.     y = r->top + finfo.ascent;
  289.     x = r->right - StringWidth((**vers).shortVers);
  290.     MoveTo(x,y);
  291.     DrawString((**vers).shortVers);
  292.     
  293.     HUnlock((Handle)vers);
  294.     ReleaseResource((Handle)vers);
  295.     
  296.     TextFont(0);
  297.     TextSize(12);
  298. }
  299.  
  300. /*    AboutMeFilter
  301.  *
  302.  *        This is the filter procedure for simply waiting around until the
  303.  *    user does something.
  304.  */
  305.  
  306. static pascal Boolean AboutMeFilter(DialogPtr dlog, EventRecord *event, short *item)
  307. {
  308.     if (GRedraw) {
  309.         SetPort(dlog);
  310.         DrawVersionString(&(dlog->portRect));
  311.         GRedraw = 0;
  312.     }
  313.     
  314.     if ((event->what == mouseDown) || (event->what == keyDown)) {
  315.         *item = 1;
  316.         return 1;
  317.     } else if (event->what == updateEvt) {
  318.         GRedraw = 1;
  319.         return 0;
  320.     } else return 0;
  321. }
  322.  
  323. /*    OpenAboutMe
  324.  *
  325.  *        Open the about me dialog box
  326.  */
  327.  
  328. static DialogPtr OpenAboutMe(void)
  329. {
  330.     DialogPtr dlog;
  331.     GrafPtr port;
  332.     Rect r;
  333.     short x,y;
  334.     
  335.     if (FColorQuickdraw) {
  336.         dlog = GetNewDialog(ColorAboutMe,NULL,(WindowPtr)-1);
  337.         if (dlog == NULL) {
  338.             dlog = GetNewDialog(BWAboutMe,NULL,(WindowPtr)-1);
  339.             if (dlog == NULL) {
  340.                 return NULL;
  341.             }
  342.         }
  343.     } else {
  344.         dlog = GetNewDialog(BWAboutMe,NULL,(WindowPtr)-1);
  345.         if (dlog == NULL) {
  346.             return NULL;
  347.         }
  348.     }
  349.     
  350.     GetWMgrPort(&port);                        // Center the window
  351.     r = port->portRect;
  352.     x = r.right - r.left;
  353.     y = r.bottom - r.top;
  354.     r = dlog->portRect;
  355.     x -= r.right - r.left;
  356.     y -= r.bottom - r.top;
  357.     x /= 2;
  358.     y /= 2;
  359.     MoveWindow(dlog,x,y,0);
  360.     
  361.     ShowWindow(dlog);
  362.     SetPort(dlog);
  363.     
  364.     return dlog;
  365. }
  366.  
  367. /*    DoAboutMe
  368.  *
  369.  *        how to run a dialog box
  370.  */
  371.  
  372. static void DoAboutMe(void)
  373. {
  374.     short i;
  375.     ModalFilterUPP proc;
  376.     
  377.     DialogPtr dlog = OpenAboutMe();
  378.     if (!dlog) return;
  379.     proc = (ModalFilterUPP)NewRoutineDescriptor((ProcPtr)AboutMeFilter,uppModalFilterProcInfo,GetCurrentISA());
  380.     
  381.     for (;;) {
  382.         ModalDialog(proc,&i);
  383.         if (i == 1) break;
  384.     }
  385.  
  386.     DisposeRoutineDescriptor(proc);
  387.     DisposeDialog(dlog);
  388. }
  389.  
  390. /************************************************************************/
  391. /*                                                                        */
  392. /*    Strip Dialog                                                        */
  393. /*                                                                        */
  394. /************************************************************************/
  395.  
  396. /*    DoStripDialog
  397.  *
  398.  *        Ask the user to open an application and strip it by calling the
  399.  *    strip routine
  400.  */
  401.  
  402. static void DoStripDialog(void)
  403. {
  404.     SFTypeList files = { 'APPL', NULL, NULL, NULL };
  405.     
  406.     if (FHasFSSpec) {
  407.         StandardFileReply reply;
  408.         
  409.         StandardGetFile(NULL,1,files,&reply);
  410.         if (reply.sfGood) StripFile(&reply.sfFile);
  411.     } else {
  412.         SFReply reply;
  413.         Point pt;
  414.         FSSpec file;
  415.         long procID;
  416.         
  417.         pt.h = 75;
  418.         pt.v = 75;
  419.         SFGetFile(pt,NULL,NULL,1,files,NULL,&reply);
  420.         if (!reply.good) return;
  421.         
  422.         procID = 0;
  423.         file.vRefNum = 0;
  424.         BlockMove(reply.fName,file.name,1+reply.fName[0]);
  425.         GetWDInfo(reply.vRefNum,&file.vRefNum,&file.parID,&procID);
  426.         StripFile(&file);
  427.     }
  428. }
  429.  
  430. /************************************************************************/
  431. /*                                                                        */
  432. /*    Event Management                                                    */
  433. /*                                                                        */
  434. /************************************************************************/
  435.  
  436. /*    GetEvent
  437.  *
  438.  *        Get the next event in a reasonable way
  439.  */
  440.  
  441. static int GetEvent(EventRecord *event)
  442. {
  443.     return WaitNextEvent(everyEvent,event,GetCaretTime(),NULL);
  444. }
  445.  
  446. /*    DoMenu
  447.  *
  448.  *        Handle dispatching the menu item to the menu manager 
  449.  */
  450.  
  451. static void DoMenu(long menuID)
  452. {
  453.     short hi,lo;
  454.     MenuHandle mh;
  455.     unsigned char buffer[256];
  456.     
  457.     hi = (short)(menuID >> 16);
  458.     lo = (short)(menuID & 0x0FFFF);
  459.     
  460.     switch (hi) {
  461.         case MApple:
  462.             switch (lo) {
  463.                 case MAppleAboutMe:
  464.                     DoAboutMe();
  465.                     break;
  466.                 default:
  467.                     mh = GetMHandle(MApple);
  468.                     GetItem(mh,lo,buffer);
  469.                     OpenDeskAcc(buffer);
  470.                     break;
  471.             }
  472.             break;
  473.         case MFile:
  474.             switch (lo) {
  475.                 case MFileStrip:
  476.                     DoStripDialog();
  477.                     break;
  478.                 case MFileQuit:
  479.                     QuitFlag = 1;
  480.                     break;
  481.             }
  482.             break;
  483.         case MEdit:
  484.             SystemEdit(lo-1);
  485.             break;
  486.     }
  487. }
  488.  
  489. /*    DoMouse
  490.  *
  491.  *        Handle mouse down event
  492.  */
  493.  
  494. static void DoMouse(EventRecord *e)
  495. {
  496.     short i;
  497.     WindowPtr w;
  498.  
  499.     i = FindWindow(e->where,&w);                // Get current window
  500.     
  501.     switch (i) {
  502.         case inMenuBar:                            // In the menu bar
  503.             DoMenu(MenuSelect(e->where));
  504.             break;
  505.         case inSysWindow:                        // In a desk accessory
  506.             SystemClick(e,w);
  507.             break;
  508.         case inContent:                            // Click in content
  509.             SelectWindow(w);
  510.             break;
  511.     }
  512. }
  513.  
  514. /*    DoEvent
  515.  *
  516.  *        What to do with this event
  517.  */
  518.  
  519. static void DoEvent(EventRecord *e)
  520. {
  521.     int err;
  522.     
  523.     switch (e->what) {
  524.         case nullEvent:
  525.             if (!RunEventLoop) QuitFlag = 1;    // Don't run this application
  526.             break;
  527.         case mouseDown:
  528.             DoMouse(e);
  529.             break;
  530.         case keyDown:
  531.         case autoKey:
  532.             if (e->modifiers & cmdKey) {
  533.                 if (e->what == keyDown) {
  534.                     DoMenu(MenuKey((char)(e->message & charCodeMask)));
  535.                 }
  536.             }
  537.             break;
  538.         case kHighLevelEvent:
  539.             err = AEProcessAppleEvent(e);        // Dispatch it...
  540.             break;
  541.     }
  542.     HiliteMenu(0);
  543. }
  544.  
  545. /************************************************************************/
  546. /*                                                                        */
  547. /*    Main entry point                                                    */
  548. /*                                                                        */
  549. /************************************************************************/
  550.  
  551. /*    main
  552.  *
  553.  *        Where it all starts.
  554.  */
  555.  
  556. int main()
  557. {
  558.     EventRecord event;
  559.     
  560.     InitMacintoshOS();
  561.     GetMacintoshEnvironment();
  562.     
  563.     QuitFlag = 0;
  564.     if (FAppleEvents) {
  565.         RunEventLoop = 1;
  566.         SawNewEvent = 0;
  567.         InitAppleEvents();
  568.     } else {
  569.         RunEventLoop = 1;
  570.         SawNewEvent = 1;
  571.         InitMenuBar();
  572.     }
  573.  
  574.     while (!QuitFlag) {
  575.         GetEvent(&event);
  576.         DoEvent(&event);
  577.     }
  578.  
  579.     EndAppleEvents();
  580.     return 0;
  581. }
  582.